package intelligentcity.model;

import intelligentcity.model.intersections.Intersection;
import intelligentcity.model.streets.Street;
import intelligentcity.model.turns.Turn;

import java.util.List;
import java.util.Random;

public class City {

    private final static Random random = new Random();

    private final int leftBorderOfTraffic;
    private final int rightBorderOfTraffic;
    private final int upperBorderOfTraffic;
    private final int lowerBorderOfTraffic;

    private final List<Street> streets;
    private final List<Intersection> intersections;
    private final List<Turn> turns;
    private final List<WeightedPoint> startPoints;
    private int sumOfWeightsOfStartPoints;

    public City(List<Street> streets, List<Intersection> intersections, List<Turn> turns,
            List<WeightedPoint> startPoints, int leftB, int rightB, int upperB, int lowerB) {

        this.streets = streets;
        this.intersections = intersections;
        this.turns = turns;
        this.startPoints = startPoints;
        sumOfWeightsOfStartPoints = 0;
        for (WeightedPoint point : startPoints) {
            sumOfWeightsOfStartPoints += point.getWeight();
        }
        this.leftBorderOfTraffic = leftB;
        this.rightBorderOfTraffic = rightB;
        this.upperBorderOfTraffic = upperB;
        this.lowerBorderOfTraffic = lowerB;

    }

    public List<Street> getStreets() {
        return streets;
    }

    public List<Intersection> getIntersections() {
        return intersections;
    }

    public List<Turn> getTurns() {
        return turns;
    }

    public List<WeightedPoint> getStartPoints() {
        return startPoints;
    }

    public Point getRandomStartPoint() {

        int value = random.nextInt(sumOfWeightsOfStartPoints);
        int index = 0;
        while (value > 0) {
            value -= startPoints.get(index++).getWeight();
        }
        if (value < 0)
            index--;
        return new Point(startPoints.get(index));
    }

    public int getLeftborderoftraffic() {
        return leftBorderOfTraffic;
    }

    public int getRightborderoftraffic() {
        return rightBorderOfTraffic;
    }

    public int getUpperborderoftraffic() {
        return upperBorderOfTraffic;
    }

    public int getLowerborderoftraffic() {
        return lowerBorderOfTraffic;
    }

}
